<?php
/**
 * Created by PhpStorm.
 * User: yanggang
 * Date: 2018/3/7 0007
 * Time: 下午 4:16
 * desc 验证用户是否已登录
 */

namespace App\Http\Middleware;

use Closure;


class Permission
{
    /**
     * @author yanggang
     * @date 2017/03/07
     * @param $request
     * @param Closure $next
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed
     */
    public function handle($request, Closure $next)
    {
        // 验证是否已经登录
        if (!$this->checkLogin($request)){
        //if (empty(session('admin_info'))) {    
            return redirect('admin/login');
        }

        return $next($request);
    }

    /**
     * $author yanggang
     * @date 2017/03/07
     * @param $request
     * @return bool
     */
    protected function checkLogin($request)
    {
        //不需要验证的控制器
        $allow_controller = ['LoginController'];

        //获取实际访问的控制器
        $controller = get_class($request->route()->getController());
        $controllerArr = explode('\\',$controller);
        $controllerName = array_pop($controllerArr);

        if (empty(session('admin_info')) && !in_array($controllerName,$allow_controller)){
            return false;
        }
        return true;
    }
}